<?php
class Crawl_yunyun_keyword_from_wap_library  extends Crawl_keyword_library{
    private $_url;
    private $_parameters = '';
    public  $_htmlsearch_config;
    private $_cookie;
    /**
     * 定义 CURL 消息头、用户端信息、抓取地址
     * 定义 新浪网页抓取选项
     */
    function __construct(){
        parent::__construct();
        $openplatforms = $this->_ci->config->item('openplatforms');
        $this->_htmlsearch_config  = $openplatforms['openplatforms']['htmlsearch']['yunyun_wap'];
        $this->_null_tab    = $this->_htmlsearch_config['null'];
        $this->_url         = $this->_htmlsearch_config['url'];

        $this->write_log_stary_tag();
    }


    public function crawl($retry = FALSE) {
        $crawl_result = $this->try_crawl($retry);
        if(is_array($crawl_result) && array_key_exists($this->_error_string, $crawl_result)) {
            return $crawl_result;
        }

        if(is_array($crawl_result) && array_key_exists('data', $crawl_result) && is_null($crawl_result['data'])) {
            return $crawl_result;
        } else {
            return $this->make_data($crawl_result);
        }
    }

    private function make_data($result = NULL) {
        $this->_ci->load->model('split/htmlsearchparser_model');

        $split_data = $this->_ci->htmlsearchparser_model->parser_yunyun($this->_htmlsearch_config, $result);
        if($split_data == FALSE) {
            log_message(self::LOG_LEVEL_WARING, 'Match And Split Data Failed!', FALSE, $this->_log_dir);
            return $this->_ci->error_code_library->getError('make_data', 'data', 'match_split_data');
        }
        return $split_data;
    }

    /**
     * 尝试抓取
     * @param bool $retry 是否使用代理和重试功能
     * @return array|string 错误时返回error数组 正确时返回抓取到的字符穿
     */

    private function try_crawl($retry = FALSE) {
        //临时数组
        $temp_array = array();
        //定义抓取参数
        $parameters = $this->make_parameters();
        if(is_array($parameters) && array_key_exists($this->_error_string, $parameters)) {
            log_message($this->_error_string, 'Crawl From YunYun WAP Make CURL Parameters Error', FALSE, $this->_log_dir);
            return $parameters;
        }
        if($retry == FALSE) {
            //不使用代理、不重试直接抓取
            log_message(self::LOG_LEVEL_NOTICE, 'Will Crawl From YunYun WAP Un Use Retry And Un Use Proxy With Parameters: '.json_encode($this->_parameters), FALSE, $this->_log_dir);
            return self::get_content($this->_parameters);
        } else {
            $crawl_use_proxy_ip_result = $this->crawl_use_proxy_ip($this->_parameters, self::PRROXY_IP, $this->_crawl_from['yunyun_wap'], TRUE);
            if(is_array($crawl_use_proxy_ip_result)) {
                if(!array_key_exists($this->_error_string, $crawl_use_proxy_ip_result) && !array_key_exists($this->_null_data_array_key, $crawl_use_proxy_ip_result)) {
                    $temp_array = $crawl_use_proxy_ip_result;
                }

                $re_crawl_result = $this->crawl_use_proxy_ip($this->_parameters, self::MOBILITY_IP, $this->_crawl_from['yunyun_wap'], TRUE);

                if(is_array($re_crawl_result) && !array_key_exists($this->_error_string, $re_crawl_result) && !array_key_exists($this->_null_data_array_key, $re_crawl_result)) {
                    return $this->make_crawl_return_array($temp_array, $re_crawl_result);
                } else if(count($temp_array) > 0) {
                    return $this->make_crawl_return_array($temp_array);
                }else {
                    return $re_crawl_result;
                }
            } else {
                return $crawl_use_proxy_ip_result;
            }
        }
    }

    /**
     * 抓取内容验证
     * @param $html
     * @return bool
     */
    public function match_html($html) {
        if(strpos($html, $this->_htmlsearch_config['no_login'])) {
            $this->label_cookie($this->_cookie['id']);
            //如果返回登陆页 重新组织参数 更新COOKIE
            $this->make_parameters();
            return FALSE;
        } elseif(strpos($html, $this->_htmlsearch_config['result_error'])) {
            $this->label_proxy_table($this->_proxy_ip, $this->_use_proxy);
            return $this->_ci->error_code_library->getError('curl', 'data', 'verification_code', $this->_htmlsearch_config['result_error']);
        } elseif(strpos($html, $this->_htmlsearch_config['result_success']) === FALSE) {
            return $this->_ci->error_code_library->getError('curl', 'data', 'incomplete_answers', $this->_htmlsearch_config['result_success']);
        } else {
            return TRUE;
        }
    }

    private function make_parameters() {
        $cookie = $this->get_cookie();

        if(is_array($cookie) && array_key_exists($this->_error_string, $cookie)) {
            return $cookie;
        }
        $this->_cookie = $cookie;
        $parameters['url'] = $this->_url;
        $parameters['method'] = self::METHOD_GET;
        $parameters['postData']['q'] = trim($this->_ci->input->post('word'));
        $parameters['postData']['p'] = $this->get_page();
        $parameters['cookie_str'] = TRUE;
        $parameters['cookie'] = $this->_cookie['cookie_str'];
        return $this->_parameters = $parameters;
    }
}